﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows;
using System.Windows.Shapes;

namespace ElectricalSymbols.Storyboards
{
	public class CloseStory
	{
		private readonly Storyboard story = new();
		public CloseStory(Line target)
		{
			InitStory(target);
		}

		private void InitStory(Line target, Storyboard? storyboard = null, double from = 0, double to = -30)
		{
			storyboard ??= story;

			// 获取线段的端点坐标
			double centerX = target.X1; // 设置旋转中心为 X1
			double centerY = target.Y1; // 设置旋转中心为 Y2

			// 创建旋转变换并设置中心点
			RotateTransform rotateTransform = new()
			{
				CenterX = centerX,
				CenterY = centerY
			};
			target.RenderTransform = rotateTransform; // 应用旋转变换

			// 创建动画
			DoubleAnimation rotationAnimation = new()
			{
				From = from, // 动画的起始角度
				To = to,     // 动画的结束角度
				Duration = TimeSpan.FromSeconds(0.5) // 动画的持续时间
			};

			// 将动画绑定到 RotateTransform 的 Angle 属性
			Storyboard.SetTarget(rotationAnimation, target);
			Storyboard.SetTargetProperty(rotationAnimation, new PropertyPath("RenderTransform.Angle"));

			// 将动画添加到故事板
			storyboard.Children.Add(rotationAnimation);
		}


		public void Run()
		{
			story.Begin(); //播放此动画
		}
	}
}
